package com.example.myprojectoffeixue;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by ASUS on 2017/1/4.
 */
public class HongWaiAdapter extends BaseAdapter {


    private ArrayList<Websocketutil> d;
    private LayoutInflater layoutInflater;
    private Context context;
    public HongWaiAdapter(Context context, ArrayList<Websocketutil> data) {
        this.context = context;
        this.d = data;
        this.layoutInflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return d.size();
    }

    public Object getItem(int position) {
        return d.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder h = null;
        if (convertView == null) {
            // 获取组件布局
            convertView = layoutInflater.inflate(R.layout.item_hw, null);
            h = new ViewHolder();
            h.tv = (TextView) convertView.findViewById(R.id.name);
            h.tv1 = (TextView) convertView.findViewById(R.id.time);
            h.tv2 = (TextView) convertView.findViewById(R.id.act);
            // 这里要注意，是使用的tag来存储数据的。
            convertView.setTag(h);
        } else {
            h = (ViewHolder) convertView.getTag();
        }
        if (position % 2 != 0) {
            convertView.setBackgroundColor(Color.parseColor("#fafbfd"));
        } else {
            convertView.setBackgroundColor(Color.parseColor("#ffffff"));
        }
        if (d.get(position).deviceName != null) {
            h.tv.setText(d.get(position).deviceName);
        } else {
                h.tv.setText("红外");
        }
            h.tv1.setText("检查到有人移动");
            if (d.get(position).endTime != null) {
                h.tv2.setText(FormartTime(d.get(position).startTime) + "-" + FormartTime(d.get(position).endTime));
            } else {
                h.tv2.setText(FormartTime(d.get(position).startTime) + "-" + FormartTime(d.get(position).startTime));


            }


        return convertView;
    }

    class ViewHolder {

        TextView tv;
        TextView tv1;
        TextView tv2;

    }


    public String  FormartTime(Long time2) {
        long time = time2;
        Date date = new Date(time);
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return  sd.format(date);
    }





























}
